package com.welsy.stats.command
{
	import com.welsy.stats.model.StatsModelLocator;
	import com.welsy.stats.vo.IterClassVO;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.stats.business.GetClassroomTrackingStatsDelegate;
	import mx.rpc.events.ResultEvent;
	import com.welsy.stats.vo.IterElementStatsVO;
	import com.welsy.stats.view.classview.CourseView;
	import com.welsy.stats.view.classview.CourseView;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.vo.IterStatsVO;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.view.classview.IterView;
	import com.welsy.stats.event.GetClassroomCourseStatsEvent;
	import com.welsy.stats.vo.TestVO;
	import com.welsy.stats.view.classview.TestView;
	import com.welsy.stats.view.classview.TestView;
	import com.welsy.stats.business.GetClassroomCourseStatsDelegate;
	import com.welsy.stats.event.GetTestStatsEvent;
	import com.welsy.stats.business.GetTestStatsDelegate;
	import com.welsy.qnames.AdminResultsDataTypes;
	import com.welsy.stats.vo.QuestionVO;
	import com.welsy.qnames.TestDataTypes;
	import com.welsy.stats.view.classview.QuestionView;
	import com.welsy.stats.vo.QuestionVO;
	import com.welsy.stats.view.classview.AnswerView;
	import com.welsy.authoring.vo.AnswerVO;
	
	public class GetTestStatsCommand implements ICommand, IResponder
	{
		[Bindable] private var smodel : StatsModelLocator = StatsModelLocator.getInstance();	
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetTestStatsDelegate = new GetTestStatsDelegate(this);
			var gts_eve : GetTestStatsEvent = GetTestStatsEvent(event);
			
			delegate.getTestStats(gts_eve.testId);
		}
		
		public function result(event : Object) :void {
			//parse result
			var xres : XMLList = XMLList(ResultEvent(event).result);
			
			var gtsr_e : XMLList = xres.descendants(AdminResultsDataTypes.TAG_GETTESTSTATSRESULT);//GetTestStatsResult
			
			var qsc_e : XMLList = gtsr_e.descendants(AdminResultsDataTypes.TAG_QUESTIONSTATSCOLLECTION).children();
			for each(var qst_e : XML in qsc_e) {
				//QuestionStatsType
				var qest_e : XMLList = qst_e.child(AdminResultsDataTypes.TAG_QUESTION);
				var questView : QuestionView = new QuestionView;
				var questVO : QuestionVO = new QuestionVO;
				questVO.questionId = qest_e.child(TestDataTypes.TAG_QUESTIONID);
				questVO.title = qest_e.child(TestDataTypes.TAG_TITLE);
				questVO.position = qest_e.child(TestDataTypes.TAG_POSITION);
				//Left the others out
				questView.questionVO = questVO;
				
				smodel.courseUsers = qst_e.child(AdminResultsDataTypes.TAG_USERSCOUNT);
				
				smodel.currentCourseTest.questions = new ArrayCollection;
				var answcoll_e : XMLList = qst_e.child(AdminResultsDataTypes.TAG_ANSWERSTATSCOLLECTION).children();
				for each(var ast_e : XML in answcoll_e) {
					//AnswerStatsType
					var answ_e : XMLList = ast_e.child(AdminResultsDataTypes.TAG_ANSWER);
					var ansView : AnswerView = new AnswerView;
					var ansVO : AnswerVO = new AnswerVO;
					ansVO.answerId = answ_e.child(TestDataTypes.TAG_ANSWERID);
					ansVO.title = answ_e.child(TestDataTypes.TAG_TITLE);
					ansVO.weight = answ_e.child(TestDataTypes.TAG_WEIGHT);

					ansView.usersCount = ast_e.child(AdminResultsDataTypes.TAG_USERSCOUNT);
					
					ansView.answerVO = ansVO;
					questView.answers.addItem(ansView);
				}
				questView.refreshChildren();
				smodel.currentCourseTest.questions.addItem(questView);
			}
			
			smodel.currentCourseTest.refreshChildren(null);
		}
		
		public function fault( event : Object ):void {
			Alert.show("GetTestStatsCommand.fault!");
		}
		
	}
}